All files / web/src/app/api/players/[id]/effective-tier route.ts

0% Statements 0/43
0% Branches 0/1
0% Functions 0/1
0% Lines 0/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                       
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { canPerformAction } from '@/lib/classroom'
import { getEffectiveTierForStudent, getLimitsForTier } from '@/lib/subscription'
import { getUserId } from '@/lib/viewer'

/**
 * GET /api/players/[id]/effective-tier
 *
 * Returns the effective subscription tier for a student,
 * considering all linked parents' plans (not just the caller's).
 *
 * Response:
 * {
 *   tier: 'family' | 'free' | 'guest',
 *   limits: { maxSessionMinutes, maxSessionsPerWeek, ... },
 *   providedBy: { name: 'Mom' } | null
 * }
 */
export const GET = withAuth(async (_request, { params }) => {
  const { id: playerId } = (await params) as { id: string }

  const userId = await getUserId()
  const canView = await canPerformAction(userId, playerId, 'view')
  if (!canView) {
    return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
  }

  const { tier, providedBy } = await getEffectiveTierForStudent(playerId, userId)
  const limits = getLimitsForTier(tier)

  return NextResponse.json({
    tier,
    limits: {
      maxPracticeStudents:
        limits.maxPracticeStudents === Infinity ? null : limits.maxPracticeStudents,
      maxSessionMinutes: limits.maxSessionMinutes,
      maxSessionsPerWeek: limits.maxSessionsPerWeek === Infinity ? null : limits.maxSessionsPerWeek,
      maxOfflineParsingPerMonth: limits.maxOfflineParsingPerMonth,
    },
    providedBy,
  })
})